﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace _3D_FileExplorer
{
    class DrawTextLabels
    {
        SpriteBatch spriteBatch;
        GraphicsDevice graphicsDevice;
        SpriteFont font;
        ContentManager content;
        Texture2D textBack;
        public DrawTextLabels(GraphicsDevice graphicsDevice, SpriteFont font, ContentManager content)
        {
            this.graphicsDevice = graphicsDevice;
            this.font = font;
            this.content = content;

            spriteBatch = new SpriteBatch(graphicsDevice);
            textBack = content.Load<Texture2D>(@"Textures\textback");


        }

        public void Draw(Camera camera, List<Primitives> cylTree, BoundingFrustum boundingFrustum)
        {
            int maxBranchLevel = ((Cylinder)cylTree[0]).branchLevel;
            Vector3 screenSpace;
            Vector2 textPosition;

            foreach (Primitives pr in cylTree)
            {
                float distance = Vector3.Distance(Fe.camera.cameraPosition, pr.center);
                float scale = 15f * 1f / distance;
                if (scale > 1.5f) scale = 1.5f;
                if ((distance < 100f) && (pr is Cube))
                {
                    #region LEAFS

                    if (boundingFrustum.Contains(pr.center) == ContainmentType.Contains)
                    {
                        // calculate screenspace of text3d space position
                        screenSpace = graphicsDevice.Viewport.Project(pr.center, camera.projection, camera.view, Matrix.Identity);

                        // get 2D position from screenspace vector
                        textPosition.X = screenSpace.X;
                        textPosition.Y = screenSpace.Y;
                        spriteBatch.Begin();
                        spriteBatch.DrawString(font, pr.type, textPosition, Color.White, 0, Vector2.Zero, scale / 4f, SpriteEffects.None, 0);
                        textPosition.X = textPosition.X - 1;
                        spriteBatch.DrawString(font, pr.type, textPosition, Color.Black, 0, Vector2.Zero, scale / 4f, SpriteEffects.None, 0);
                        spriteBatch.End();
                    }

                    #endregion
                }
            }
            foreach (Primitives pr in cylTree)
            {
                if (pr is Cylinder)
                {
                    float distance = Vector3.Distance(Fe.camera.cameraPosition, pr.center);

                    #region CYLINDERS
                    if (((Cylinder)pr).branchLevel >= maxBranchLevel - 1)
                    {
                        if (pr.weight > 1.5)
                        {
                            if ((distance < 500) && (distance > 80f))
                            {
                                float scale = 0.008f * distance;
                                if (scale > 1f) scale = 1f;
                                if (boundingFrustum.Contains(pr.center) == ContainmentType.Contains)
                                {
                                    screenSpace = graphicsDevice.Viewport.Project(pr.center, camera.projection, camera.view, Matrix.Identity);

                                    //get 2D position from screenspace vector
                                    textPosition.X = screenSpace.X;
                                    textPosition.Y = screenSpace.Y;

                                    //calculate scale
                                    float cylScale = 1f /12f * pr.weight * scale;
                                    Vector2 textBackScale;
                                    textBackScale.X = pr.shortName.Length * 0.0065f * (1.5f - (pr.shortName.Length * 0.05f)) * pr.weight * scale;
                                    textBackScale.Y = 0.07f * pr.weight * scale;
                                    //draw
                                    spriteBatch.Begin();
                                    spriteBatch.Draw(textBack, textPosition, null, new Color(Color.Orange, 0.8f), 0, Vector2.Zero, textBackScale, SpriteEffects.None, 1);
                                    textPosition.X += 1f;
                                    spriteBatch.DrawString(font, pr.shortName, textPosition, Color.Black, 0, Vector2.Zero, cylScale, SpriteEffects.None, 1);
                                    spriteBatch.End();

                                }
                            }
                        }
                        else if (pr.weight > 1.3)
                        {
                            if ((distance < 150) && (distance > 30f))
                            {
                                float scale = 0.1f * distance;
                                if (scale > 1f) scale = 1f;
                                if (boundingFrustum.Contains(pr.center) == ContainmentType.Contains)
                                {
                                    screenSpace = graphicsDevice.Viewport.Project(pr.center, camera.projection, camera.view, Matrix.Identity);

                                    //get 2D position from screenspace vector
                                    textPosition.X = screenSpace.X;
                                    textPosition.Y = screenSpace.Y;

                                    //calculate scale
                                    float cylScale = 1f / 10f * pr.weight * scale;
                                    Vector2 textBackScale;
                                    textBackScale.X = pr.shortName.Length * 0.009f * (1.5f - (pr.shortName.Length * 0.05f)) * pr.weight * scale;
                                    textBackScale.Y = 0.07f * pr.weight * scale;
                                    //draw
                                    spriteBatch.Begin();
                                    spriteBatch.Draw(textBack, textPosition, null, new Color(Color.Orange,0.8f), 0, Vector2.Zero, textBackScale, SpriteEffects.None, 1);
                                    textPosition.X += 1f;
                                    spriteBatch.DrawString(font, pr.shortName, textPosition, Color.Black, 0, Vector2.Zero, cylScale, SpriteEffects.None, 1);
                                    spriteBatch.End();

                                }
                            }
                        }
                        else if (pr.weight <= 1.3)
                        {
                            if ((distance < 80) && (distance > 30f))
                            {
                                float scale = 0.1f * distance;
                                if (scale > 1f) scale = 1f;
                                if (boundingFrustum.Contains(pr.center) == ContainmentType.Contains)
                                {
                                    screenSpace = graphicsDevice.Viewport.Project(pr.center, camera.projection, camera.view, Matrix.Identity);

                                    //get 2D position from screenspace vector
                                    textPosition.X = screenSpace.X;
                                    textPosition.Y = screenSpace.Y;

                                    //calculate scale
                                    float cylScale = 1f / 7f * pr.weight * scale;
                                    Vector2 textBackScale;
                                    textBackScale.X = pr.shortName.Length * 0.012f * (1.5f - (pr.shortName.Length * 0.05f)) * pr.weight * scale;
                                    textBackScale.Y = 0.1f * pr.weight * scale;
                                    //draw
                                    spriteBatch.Begin();
                                    spriteBatch.Draw(textBack, textPosition, null, new Color(Color.Orange, 0.7f), 0, Vector2.Zero, textBackScale, SpriteEffects.None, 1);
                                    textPosition.X += 1f;
                                    spriteBatch.DrawString(font, pr.shortName, textPosition, Color.Black, 0, Vector2.Zero, cylScale, SpriteEffects.None, 1);
                                    spriteBatch.End();

                                }
                            }
                        }
                    }
                    #endregion
                }


            }
        }

    }
}
